package myJs.projectCps

import myJs.Implicits._
import myJs.api.Api
import myJs.cps.BootstrapTableCp.Column
import myJs.cps._
import myJs.tool.Pojo._
import myJs.tool.{Pojo, Tool}
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

import scala.collection.immutable.SeqMap
import scala.scalajs.js.Dynamic.{global => g}
import japgolly.scalajs.react._
import myJs.myPkg.Hooks._
import japgolly.scalajs.react.vdom.all._
import myJs.Utils._

/** Created by yz on 21/1/2022
  */
object AnnoTableCp {

  case class Props(keyword: String){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val (columns, setColumns) = useState(List.empty[Column])
    val (datas, setDatas) = useState(List.empty[ujson.Obj])
    val (dbRsNumbers, setDbRsNumbers) = useState(List[String]())

    object FTool {

      def getColumns(infoMap: SeqMap[String, String]): List[Column] = {
        val map = infoMap
        val basicColumns = map.map { case (k, v) =>
          val searchKind = k match {
            case "frequency" | "publications" => NumericSearch
            case "variationType" | "clinicalSignificance" => CheckboxSearch
            case _ => TextSearch
          }
          val fmt = FTool.tbFmt(k)
          val comVF = FTool.comVFmt(k)
          val optionMap = searchKind match {
            case Pojo.CheckboxSearch => datas.map(_(k).myStr).distinct.sorted.optionMap
            case _ => SeqMap[String, String]()
          }
          Column(
            field = k,
            title = v,
            operable = true,
            searchKind = searchKind,
            formatter = fmt,
            comVF = comVF,
            optionMap = optionMap
          )
        }.toList
        basicColumns
      }

      def tbFmt(field: String) = (v: ujson.Value, row: ujson.Obj) => {
        val aStyle = TagMod(
          cursor.pointer
        )
        field match {
          case "rsNumber" =>
            val detailUrl = g.jsRoutes.controllers.DataController.detailInfoBefore().url.toString
            a(
              title := "Detail",
              href := s"${detailUrl}?rsNumber=${v.myStr}",
              aStyle,
              target := "_blank",
              v.myStr
            )()
          case _ => span(FTool.comVFmt(field)(v, row))()
        }
      }

      def comVFmt(fileTitle: String) = (v: ujson.Value, row: ujson.Obj) => {
        fileTitle match {
          case "frequency" =>
            val vs = v.arr
            s"${vs.size}"
          case "publications" =>
            s"${v.myStr.split("\\s+").head.toInt}"
          case _ => v.myStr
        }
      }

      def refreshTableB = {
        Api.anno.all.flatMapSync { response =>
          setDatas(response)
        }
      }

      def refreshDbRsNumbers = {
        Api.data.allRsNumber.flatMapSync { response =>
          setDbRsNumbers(response)
        }
      }

    }

    useEffect(
      {
        FTool.refreshDbRsNumbers.toCallback >>
        FTool.refreshTableB.toCallback
      },
      List()
    )

    useEffect(
      {
        val map = Tool.annoMap
        val curColumns = FTool.getColumns(map)
        setColumns(curColumns)
      },
      List(dbRsNumbers, datas)
    )

    div(
      div(

        BootstrapTableCp.Props(
          columns = columns,
          datas = datas,
          pageList = List(10, 25, 50),
          pageSize = 10,
          search = true,
          allSearch = props.keyword
        ).render
      )
    )

  }

}
